﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using Database.Classes;

public partial class UserDefinedFunctions
{
    public static bool ChangePassword(int userId, string password)
    {
        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Update Players Set Password = @p Where PlayerId = @pId", conn))
            {
                cmd.Parameters.Add("@pId", SqlDbType.Int).Value = userId;
                cmd.Parameters.Add("@p", SqlDbType.NVarChar).Value = StaticMethods.GetMd5Sum(UserDefinedFunctions.PlayerName(userId) + password);
              int i =  cmd.ExecuteNonQuery();
              if (i > 0) return true;
              else return false;
            }
        }

    }
    public static Boolean AuthorizeUser(string user, string password)
    {

        using (SqlConnection conn = new SqlConnection(StaticMethods.ConnectionString()))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("Select Password From Players Where PlayerName = @pn", conn))
            {
                cmd.Parameters.Add("@pn", SqlDbType.NVarChar).Value = user;
                object o = cmd.ExecuteScalar();
                if (o == null)
                    return false;

                string tmp = o.ToString();
                if (tmp == StaticMethods.GetMd5Sum(user + password))
                    return true;
                else
                    return false;
            }
        }
    }
};

